home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2642 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  83 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: File Size in bytes.
  5. Date: 22 Jan 1996 22:10:43 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan22171044@g7240065.bridge.bst.bls.com>
  8. References: <4e0r59$733@hammerhead.dadd.ti.com>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: Sudheer Vemulapalli's message of 22 Jan 1996 20:13:29 GMT
  11.  
  12. In article <4e0r59$733@hammerhead.dadd.ti.com> Sudheer Vemulapalli <sudheer@dadd.ti.com> writes:
  13.  
  14. : Is there a simple way to find the exact size of a file. I would like to know of
  15. : a way to do it other than using ls -l and getting the size field from the
  16. : output. If there is a UNIX system call or somefunction that returns the file
  17. : size please let me know.
  18.  
  19. Though this is phrased in a UNIX specific way (and as such the poster
  20. should perhaps have asked his question in comp.unix.programmer) it is
  21. a more general question than that.
  22.  
  23. In ANSI 'C' the closest you can get is:
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. int
  29. main(void)
  30. {
  31.     FILE* fp;
  32.     long size;
  33.   
  34.     if ((fp = fopen("SomeFile", "rb")) == 0)
  35.         return EXIT_FAILURE;
  36.  
  37.     if (fseek(fp, 0L, SEEK_END) != 0)
  38.         return EXIT_FAILURE;
  39.  
  40.     if ((size = ftell(fp)) == -1)
  41.         return EXIT_FAILURE;
  42.  
  43.     printf("File size = %ld\n", size);
  44.  
  45.     return EXIT_SUCCESS;
  46. }
  47.  
  48. The file needs to be opened in binary mode (which means absolutely nothing
  49. on the UNIX operating system) for ftell. For a binary stream, the value 
  50. returned by ftell is the number of characters from the beginning of the file.
  51. Unfortunately on binary streams the standard states that fseek need not
  52. meaningfully support calls with SEEK_END. It does not say the fseek
  53. call fails in such situations so in effect this program could print an
  54. arbitrary value. On most implementations an fseek on a stream, when the
  55. stream points to a file, should in fact work.
  56.  
  57. A non-portable (depends on the defintion of portable I suppose) UNIX based
  58. solution:
  59.  
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <sys/stat.h>
  63.  
  64. int
  65. main(void)
  66. {
  67.     struct stat st;
  68.  
  69.     if (stat("SomeFile", &st) == -1)
  70.         return EXIT_FAILURE;
  71.  
  72.     printf("File size = %ld\n", (long)st.st_size);
  73.  
  74.     return EXIT_SUCCESS;
  75. }
  76.  
  77. Hope one of these helps
  78. Regards
  79.  
  80.     -A.
  81. -- 
  82. | A.Champion                |
  83.